﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WorkWeChat
{
    public class Http
    {
        public static string RequestPost(string url, string data)
        {
            var html = string.Empty;
            var request = (HttpWebRequest)WebRequest.Create(url);
            var postData = Encoding.UTF8.GetBytes(data);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;
            try
            {
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(postData, 0, postData.Length);
                }
                var response = (HttpWebResponse)request.GetResponse();
                using (var resStream = response.GetResponseStream())
                {
                    if (resStream != null)
                        html = new StreamReader(resStream).ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                html = "error:" + e.Message;
            }
            return html;
        }

        public static string RequestGet(string url)
        {
            var html = string.Empty;
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                using (var resStream = response.GetResponseStream())
                {
                    if (resStream != null)
                        html = new StreamReader(resStream).ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                html = "error:" + e.Message;
            }
            return html;
        }

    }
}